/*     */ package com.newcrm.webapi.sub;
/*     */ 
/*     */ import com.newcrm.bean.CompanyInfo;
/*     */ import com.newcrm.bean.User;
/*     */ import com.newcrm.db.UserManager;
/*     */ import com.newcrm.exception.DBException;
/*     */ import com.newcrm.exception.NotFoundException;
/*     */ import com.newcrm.util.Util;
/*     */ import com.newcrm.webapi.AbstractAction;
/*     */ import com.newcrm.webapi.AbstractHttpServlet;
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.sql.SQLException;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import javax.servlet.RequestDispatcher;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import org.apache.commons.fileupload.FileItem;
/*     */ import org.apache.commons.fileupload.FileUploadException;
/*     */ import org.apache.commons.fileupload.disk.DiskFileItemFactory;
/*     */ import org.apache.commons.fileupload.servlet.ServletFileUpload;
/*     */ import org.apache.log4j.Logger;
/*     */ 
/*     */ public class UpdateIdentity extends AbstractAction
/*     */ {
/*  31 */   private static Logger logger = Logger.getLogger(UpdateIdentity.class
/*  32 */     .getName());
/*     */ 
/*  34 */   private String id = "";
/*  35 */   private String path = "";
/*     */ 
/*     */   public UpdateIdentity(AbstractHttpServlet servlet) {
/*  38 */     super(servlet);
/*     */   }
/*     */ 
/*     */   public UpdateIdentity(AbstractHttpServlet servlet, String id, String uploadPath)
/*     */   {
/*  43 */     super(servlet);
/*  44 */     this.id = id;
/*  45 */     this.path = uploadPath;
/*     */   }
/*     */ 
/*     */   public void parse(HttpServletRequest request, HttpServletResponse response)
/*     */     throws IOException, ServletException, NotFoundException
/*     */   {
/*  52 */     if ((this.id == null) || (this.id.trim().length() == 0)) {
/*  53 */       request.getRequestDispatcher("/errorpage.jsp").forward(request, 
/*  54 */         response);
/*  55 */       return;
/*     */     }
/*     */ 
/*  59 */     User loginUser = super.getLoginUser(request);
/*  60 */     if ((!loginUser.userId.equals(this.id)) && 
/*  61 */       (!loginUser.isAdmin) && (!loginUser.isGroupAdmin)) {
/*  62 */       request.getRequestDispatcher("/errorpage.jsp").forward(request, 
/*  63 */         response);
/*  64 */       return;
/*     */     }
/*     */ 
/*  68 */     if (("admin".equals(this.id)) && 
/*  69 */       (!"admin".equals(loginUser.userId))) {
/*  70 */       request.getRequestDispatcher("/errorpage.jsp").forward(request, 
/*  71 */         response);
/*  72 */       return;
/*     */     }
/*     */ 
/*  77 */     if ((!loginUser.userId.equals(this.id)) && (!loginUser.isAdmin)) {
/*  78 */       CompanyInfo company = super.getCompany(request);
/*  79 */       User updateUser = company.getUser(this.id);
/*  80 */       if (updateUser == null) {
/*  81 */         request.getRequestDispatcher("/errorpage.jsp").forward(request, 
/*  82 */           response);
/*  83 */         return;
/*     */       }
/*     */ 
/*  86 */       if (!company.isGroupAdmin(loginUser, updateUser.groupId)) {
/*  87 */         request.getRequestDispatcher("/errorpage.jsp").forward(request, 
/*  88 */           response);
/*  89 */         return;
/*     */       }
/*     */     }
/*     */ 
/*  93 */     DiskFileItemFactory fac = new DiskFileItemFactory();
/*  94 */     ServletFileUpload upload = new ServletFileUpload(fac);
/*  95 */     upload.setHeaderEncoding("utf-8");
    List fileList;
/*     */     try
/*     */     {
/*  98 */       fileList = upload.parseRequest(request);
/*     */     }
/*     */     catch (FileUploadException e1)
/*     */     {
/*     */
/* 100 */       e1.printStackTrace();
/*     */       return;
/*     */     }
/* 104 */     Iterator it = fileList.iterator();
/*     */ 
/* 106 */     String name = "";
/* 107 */     int sex = 1;
/* 108 */     String idCard = "";
/* 109 */     String mobile = "";
/* 110 */     String qq = "";
/* 111 */     String password = "";
/* 112 */     String comfirmPassword = "";
/* 113 */     String email = "";
/* 114 */     String imgPath = "";
/* 115 */     while (it.hasNext()) {
/* 116 */       FileItem item = (FileItem)it.next();
/* 117 */       if (item.isFormField()) {
/* 118 */         String id = item.getFieldName();
/* 119 */         if ("identity_name".equals(id)) {
/* 120 */           name = Util.getURLParameter(item.getString());
/*     */         }
/* 124 */         else if ("identity_sex".equals(id)) {
/* 125 */           sex = Util.getURLIntParameter(item.getString());
/*     */         }
/* 129 */         else if ("identity_id".equals(id)) {
/* 130 */           idCard = Util.getURLParameter(item.getString());
/*     */         }
/* 134 */         else if ("identity_mobile".equals(id)) {
/* 135 */           mobile = Util.getURLParameter(item.getString());
/*     */         }
/* 139 */         else if ("identity_qq".equals(id)) {
/* 140 */           qq = Util.getURLParameter(item.getString());
/*     */         }
/* 144 */         else if ("identity_email".equals(id)) {
/* 145 */           email = Util.getURLParameter(item.getString());
/*     */         }
/* 148 */         else if ("identity_password".equals(id)) {
/* 149 */           password = Util.getURLParameter(item.getString());
/*     */         }
/* 152 */         else if ("identity_password_confirmation".equals(id)) {
/* 153 */           comfirmPassword = Util.getURLParameter(item.getString());
/*     */         }
/*     */ 
/*     */       }
/*     */       else
/*     */       {
/* 159 */         String fileName = item.getName();
/* 160 */         if ((fileName != null) && (!fileName.trim().equals("")))
/*     */         {
/* 164 */           String extName = "";
/* 165 */           if (fileName.lastIndexOf(".") >= 0) {
/* 166 */             extName = fileName.substring(fileName.lastIndexOf("."));
/*     */           }
/*     */ 
/* 169 */           if (!Util.isImageFile(extName, item.get())) {
/* 170 */             logger.warn("file is not image " + fileName);
/* 171 */             request.getRequestDispatcher("/errorpage.jsp").forward(request, 
/* 172 */               response);
/* 173 */             return;
/*     */           }
/* 175 */           extName = extName.toLowerCase();
/*     */ 
/* 177 */           imgPath = this.id + extName;
/*     */ 
/* 179 */           File file = new File(this.path, imgPath);
/*     */           try {
/* 181 */             item.write(file);
/*     */           } catch (Exception e) {
/* 183 */             logger.warn("save image file failed", e);
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/* 187 */     if ((password != null) && (!password.equals(comfirmPassword)))
/*     */     {
/* 189 */       logger.warn("password is not match");
/* 190 */       request.getRequestDispatcher("/errorpage.jsp").forward(request, 
/* 191 */         response);
/* 192 */       return;
/*     */     }
/* 194 */     UserManager manager = new UserManager();
/*     */     try {
/* 196 */       int result = manager.update(this.id, name, sex, idCard, mobile, qq, 
/* 197 */         email, imgPath, password);
/* 198 */       if (result > 0)
/*     */       {
/* 200 */         User user = super.getLoginUser(request);
/* 201 */         user.name = name;
/* 202 */         user.email = email;
/* 203 */         if ((imgPath != null) && (imgPath.trim().length() > 0)) {
/* 204 */           user.imgPath = ("images/upload/identity/" + imgPath);
/*     */         }
/*     */ 
/* 208 */         CompanyInfo company = super.getCompany(loginUser.companyId);
/* 209 */         company.users = manager.getCompanyUsers(loginUser.companyId, 
/* 210 */           null);
/*     */       }
/* 212 */       response.sendRedirect(request.getContextPath() + "/users/" + 
/* 213 */         this.id + "/edit");
/*     */     } catch (DBException e) {
/* 215 */       logger.warn("update user basic info failed", e);
/*     */     } catch (SQLException e) {
/* 217 */       logger.warn("update user basic info failed", e);
/*     */     }
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.webapi.sub.UpdateIdentity
 * JD-Core Version:    0.6.1
 */